#! /bin/bash 

# install zabbix-agent for all sub-node, default use 'apt intall'
basepath=$(cd `dirname $0`; pwd)
confpath="/etc/zabbix/zabbix_agentd.conf"

IPS=()
Hostnames=()
Server="10.131.18.162"

# read all configs
idx=0
while read a b; do
    IPS[$idx]=$a
    Hostnames[$idx]=$b
    idx=`expr $idx + 1`
done < $basepath/sub-nodes.txt


for ((i=0;i<${#IPS[@]};i++)); do
    # use sed replace configuration, not modify origin config
    (cat ${basepath}/zabbix_agentd.conf; echo "Hostname=${Hostnames[i]}") > ${basepath}/tmp.conf
    cat ${basepath}/tmp.conf | tail -3
    rsync -aPq ${basepath}/tmp.conf root@${IPS[i]}:$confpath
    rm ${basepath}/tmp.conf

    ssh root@${IPS[i]} "systemctl restart zabbix-agent"
done